package com.cc.wechat.websocket.config;

import com.cc.wechat.websocket.handler.WebSocketHandler;
import com.cc.wechat.websocket.interceptor.WebSocketInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketConfigurer {

    private final WebSocketHandler webSocketHandler;
    private final WebSocketInterceptor webSocketInterceptor;

    /**
     * 注册WebSocket处理器
     * @param registry WebSocket处理器注册器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/ws/chat")// 注册WebSocket处理器
                .addInterceptors(webSocketInterceptor)// 添加拦截器
                .setAllowedOrigins("*"); // 实际生产环境中应该限制允许的域名
    }
} 